package com.meta.nd.util;

import com.meta.cache.NdCacheService;
import com.meta.counter.service.CounterService;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class SpringBeanUtil implements ApplicationContextAware {

    private static ApplicationContext ctx;


    @Override
    public void setApplicationContext(ApplicationContext applicationcontext)
            throws BeansException {
        ctx = applicationcontext;
    }

    public static Object getBean(String id) {
        if (ctx == null) {
            throw new NullPointerException("ApplicationContext is null");
        }
        return ctx.getBean(id);
    }

    public static NdCacheService getNdCacheService(){
       return (NdCacheService)getBean("ndCacheServiceImpl");
    }

    public static Object getNvService(){
        return getBean("nvServiceImpl");
    }

    public CounterService getCounterService() {
        return (CounterService) getBean("counterServiceImpl");
    }

}
